/*----------------------------------------------- Marlin Toolkit--------------------------------------*
Authors : Daniel Randle, Alex Bourne
Date Created : Feb 20 2013

File:
	toolkit.cpp

Description:
	This file contains the definition for the Toolkit class functions along with any
	helper functions associated with the class
*-----------------------------------------------------------------------------------------------------*/


// Includes
#include <QApplication>
#include <global.h>
#include <toolkit.h>
#include <debug.h>
#include <mainwindow.h>
#include <debugwindow.h>
#include <mapwindow.h>
#include <nsengine.h>
#include <fstream>



/*--------------------*
 |class Toolkit BEGINS|
 *--------------------*/


/* Constructor
Description:
	Create and initialize all parts of the toolkit

Parameters:
	None

Return:
	None
*/
Toolkit::Toolkit(int argc, char ** argv)
{
	// Start qt app loop
	LogFile::writeDate("Logs/enginedebug.log");
	app = new QApplication(argc, argv); // Set up with command line args

	_setupShaders();
	_setupObjects();
	_setupMapAreas();
	_setupGUI();

	#ifdef DEBUG
	_setupDebug();
	#endif
}


/* Destructor
Description:
	Destroys objects as needed

Parameters:
	None

Return:
	None
*/
Toolkit::~Toolkit()
{
	LogFile("Deleting Toolkit...");
	delete engine;
	#ifdef DEBUG
	delete debug;
	#endif
	LogFile("Toolkit Deleted");
}

/*---------------------------------------- PRIVATE FUNCTIONS ------------------------------------------*/

/* _setupMap
Description:
	Set up and initialization of the map with any and all layers needed - map view responsible for
	drawing the map. Map responsible for updates.

Parameters:
	None

Return:
	None
*/
void Toolkit::_setupMapAreas()
{
}

/* _setupTileSet
Description:
	Set up and initialization of the tile set which contains all of the map tiles..
	must come before gui setup to give the tiles a display. Contains all of the available
	tiles in the game and displays them in a view in Qt.. should support click select and
	multiple selection tools. 

Parameters:
	None

Return:
	None
*/
void Toolkit::_setupObjects()
{
}

/* _setupEntitySet
Description:
	Set up and initialization of all entities in the game - set up after gui so that the drawing
	functions have a display they can draw to. The entity set will contain all of the available
	game objects that can be inserted in to a map, including landscape objects - weapon objects -
	powerup objects and etc..

Parameters:
	None

Return:
	None
*/
void Toolkit::_setupShaders()
{
}

/* _setupGUI
Description:
	Set up and initialization of all windows that are part of the GUI
	Also get their device pointers for storage and assign the models to each display

Parameters:
	None

Return:
	None
*/
void Toolkit::_setupGUI()
{
	mainWin = new MainWindow(this);
	engine = mainWin->getMapWindow()->getEngine();
	mainWin->setEngine(engine);
}

/* _setupDebug
Description:
	Set up and initialization of debug object

Parameters:
	None

Return:
	None
*/
#ifdef DEBUG
void Toolkit::_setupDebug()
{
	debug = new Debug("Logs/debug.log");
	mainWin->setDebug(debug);
}
#endif

/*----------------------------------------- PUBLIC INTERFACE ------------------------------------------*/


void Toolkit::loadPluginFromFile(const QString & fileName)
{
	std::ifstream in;
	in.open(fileName.toStdString().c_str(), std::ios::in | std::ios::binary);
	engine->clearMap();
	engine->deserializeMap(in);
	in.close();
	mainWin->myUpdate();
}

void Toolkit::savePluginToFile(const QString & fileName)
{
	std::ofstream out;
	out.open(fileName.toStdString().c_str(), std::ios::out | std::ios::binary);
	engine->serializeMap(out);
	out.close();
	mainWin->myUpdate();
}

/* start
Description:
	Starts the toolkit by using the qt exec function. Command line variables created when the toolkit
	object was created

Parameters:
	None

Return:
	None
*/
int Toolkit::start()
{
	return app->exec();
}